package com.dm.cloud.images.util;

import cn.hutool.extra.ssh.JschUtil;
import cn.hutool.extra.ssh.Sftp;
import cn.hutool.extra.ssh.SshjSftp;
import com.dm.cloud.core.exception.CustomException;

import java.io.*;
import java.nio.charset.Charset;

public class SftpClient {
    /**
     * ip
     */
    private String sshHost;
    /**
     * 端口
     */
    private int sshPort;
    /**
     * 账号
     */
    private String sshUser;
    /**
     * 密码
     */
    private String sshPass;

    private Sftp sftp;

    private SshjSftp sshjSftp;

    public SftpClient(String sshHost, int sshPort, String sshUser, String sshPass){
//        sftp= JschUtil.createSftp(sshHost,sshPort,sshUser,sshPass);
//        sshjSftp= new SshjSftp(sshHost,sshPort,sshUser,sshPass);
        this.sshHost=sshHost;
        this.sshPort=sshPort;
        this.sshUser=sshUser;
        this.sshPass=sshPass;
    }

    public void close(){
        try {
            sftp.close();
        }catch (Exception e){
            e.getMessage();
        }
        try {
            sshjSftp.close();
        }catch (Exception e){
            e.getMessage();
        }
    }

    /**
     * 初始化sftp连接
     */
    private void initSftp(){
        if(sftp==null){
            sftp= JschUtil.createSftp(sshHost,sshPort,sshUser,sshPass);
        }
    }

    /**
     * 初始化ssh
     */
    private void sshjSftp(){
        if(sshjSftp==null){
            sshjSftp= new SshjSftp(sshHost,sshPort,sshUser,sshPass);
        }

    }

    /**
     * 是否为文件
     * @param path
     * @return
     */
    public Boolean isDir(String path){
        initSftp();
        return sftp.isDir(path);
    }

    /**
     * 取文件内容
     * @param filePath
     * @param charset
     * @return
     */
    public String getFileContent(String filePath, Charset charset){
        initSftp();
        if(sftp.exist(filePath) && !sftp.isDir(filePath)){
            ByteArrayOutputStream out=new ByteArrayOutputStream();
            sftp.get(filePath,out);
            try {
                String content=out.toString(charset.name());
                return content;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                throw new CustomException("内容转换字符串失败!");
            }finally {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 推送文本内容
     * @param filePath
     * @param content
     * @param charset
     * @return
     */
    public Boolean putContent(String filePath, String content,Charset charset){
        initSftp();
        filePath=filePath.replace("\\","/");
        if(!sftp.exist(filePath) || sftp.isDir(filePath)){
            ByteArrayInputStream in=new ByteArrayInputStream(content.getBytes(charset));
            String fileName=filePath.substring(filePath.lastIndexOf("/")+1);
            String fileDir=filePath.substring(0,filePath.lastIndexOf("/"));
            try {
                return sftp.upload(fileDir,fileName,in);
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    /**
     * 上传或替换文件
     * @param remoteFilePath
     * @param localFile
     * @return
     */
    public Boolean putFile(String remoteFilePath, File localFile){
        initSftp();
        remoteFilePath=remoteFilePath.replace("\\","/");
        return sftp.upload(remoteFilePath,localFile);
    }

    /**
     * 执行命令获取返回值
     * @param command
     * @return
     */
    public String command(String command){
        sshjSftp();
        String re = sshjSftp.command(command);
        return re;
    }

    /**
     * 移动文件
     * @param src
     * @param target
     * @return 返回是空字符串证明成功
     */
    public String move(String src,String target){
        String commandStr="mv "+src+" "+target;
        return command(commandStr);
    }

}
